#!/bin/sh

# Slackware build script for Bygfoot
# Written by Jakob Nylin jakob.nylin@gmail.com

PRGNAM=bygfoot
VERSION=${VERSION:-2.2.1}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

SRCNAM=$VERSION-source

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
    SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then  
    SLKCFLAGS="-O2 -march=i686 -mtune=i686" 
elif [ "$ARCH" = "x86_64" ]; then
    SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $TMP/$PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$SRCNAM.tar.bz2
cd $PRGNAM-$SRCNAM
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG

find $PKG | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  AUTHORS COPYING ChangeLog INSTALL README ReleaseNotes TODO UPDATE \
    $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;

# Add the icon and a desktop file
mkdir -p $PKG/usr/share/{pixmaps,applications}
cat support_files/pixmaps/bygfoot_icon.png > $PKG/usr/share/pixmaps/bygfoot.png
cat $CWD/bygfoot.desktop > $PKG/usr/share/applications/bygfoot.desktop

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
